package com.andy.wan.android.mvp.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.andy.ui.base.BaseListFragment;
import com.andy.ui.base.SimpleFragmentActivity;
import com.andy.ui.base.SimpleWebActivity;
import com.andy.ui.widget.PageStateView;
import com.andy.wan.android.R;
import com.andy.wan.android.mvp.api.WanAndroidApi;
import com.andy.wan.android.mvp.bean.BaseResponse;
import com.andy.wan.android.mvp.bean.FeedArticleData;
import com.andy.wan.android.mvp.bean.FeedArticleListData;
import com.andy.wan.android.mvp.net.RetrofitUtil;
import com.andy.wan.android.mvp.ui.adapter.FeedArticleAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import io.reactivex.Observable;

/**
 * @Author: luofan
 * @CreateDate: 2020/3/31 15:40
 * @Description:
 */
public class FeedArticleListFragment extends BaseListFragment<BaseResponse<FeedArticleListData>, FeedArticleData> {

    /**
     * 项目
     */
    public static final int TYPE_PROJECT = 0;
    /**
     * 知识体系
     */
    public static final int TYPE_KNOWLEDGE = 1;

    /**
     * 微信公众号
     */
    public static final int TYPE_WX_ARTICLE = 2;

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout mSmartRefresh;
    @BindView(R.id.page_stat_view)
    PageStateView mPageStatView;

    private static final String TYPE = "type";
    private static final String CID = "cid";
    private static final String TITLE = "title";
    @BindView(R.id.img_back)
    ImageView mImgBack;
    @BindView(R.id.tv_title_center)
    TextView mTvTitleCenter;

    private int mType;

    private int mCId;

    private String mTitle;

    @Override
    public void initData() {
        mType = getArguments().getInt(TYPE);
        mCId = getArguments().getInt(CID);
        mTitle = getArguments().getString(TITLE);
    }

    @Override
    public void initLogic() {
        super.initLogic();
        mImgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
        mTvTitleCenter.setText(mTitle);
    }

    @NonNull
    @Override
    protected Observable<BaseResponse<FeedArticleListData>> getObservable() {
        switch (mType) {
            case TYPE_PROJECT:
                return RetrofitUtil.getInstance().create(WanAndroidApi.class).getProjectListData(getPageNum(), mCId);
            case TYPE_WX_ARTICLE:
                return RetrofitUtil.getInstance().create(WanAndroidApi.class).getWxSumData(getPageNum(), mCId);
            case TYPE_KNOWLEDGE:
            default:
                return RetrofitUtil.getInstance().create(WanAndroidApi.class).getKnowledgeHierarchyDetailData(getPageNum(), mCId);
        }
    }

    @Override
    protected List<FeedArticleData> getRecyclerDataFromResult(BaseResponse<FeedArticleListData> response) {
        return response.getData().getDatas();
    }

    @NonNull
    @Override
    protected RecyclerView getRecyclerView() {
        return mRecyclerView;
    }

    @Nullable
    @Override
    protected SmartRefreshLayout getRefreshLayout() {
        return mSmartRefresh;
    }

    @NonNull
    @Override
    protected BaseQuickAdapter<FeedArticleData, BaseViewHolder> getAdapter() {
        FeedArticleAdapter projectAdapter = new FeedArticleAdapter();
        projectAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                FeedArticleData bean = projectAdapter.getItem(position);
                if (bean != null) {
                    SimpleWebActivity.startMe(getContext(), bean.getLink(), bean.getTitle());
                }
            }
        });
        return projectAdapter;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_feed_article_list;
    }

    @Override
    protected PageStateView getPageStateView() {
        return mPageStatView;
    }

    public static void startMe(Context context, String title, int type, int cid) {
        Bundle bundle = new Bundle();
        bundle.putInt(TYPE, type);
        bundle.putInt(CID, cid);
        bundle.putString(TITLE, title);
        SimpleFragmentActivity.startMe(context, FeedArticleListFragment.class.getName(), bundle);
    }
}
